package com.wandian.view.stage
{
	import com.wandian.core.BitmapManager;
	import com.wandian.core.LayerManager;
	import com.wandian.data.GameData;
	import com.wandian.util.Res;
	import com.wandian.util.UIHelper;
	import com.wandian.view.item.MouseClickEffect;
	import com.wandian.view.layer.GameLayer;
	import com.wandian.view.stage.StageBase;
	import com.wandian.view.ui.UIMain;
	import com.wandian.view.ui.UIPopupLayer;
	import com.wandian.view.ui.UITaskList;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.system.ApplicationDomain;

	import ghostcat.display.game.Display45Util;


	/**
	 * 加载完资源之后，切换到游戏场景
	 * @author long
	 */
	public class StageGame extends StageBase
	{
		/**
		 * 游戏层，游戏中的元素层
		 */
		private var gameLayer:GameLayer;
		/**
		 * 游戏ui界面，小地图，头像，底部栏
		 */
		private var mainUI:UIMain;

		public function StageGame()
		{
			super();
//			if (ApplicationDomain.currentDomain.hasDefinition("btn_Spin"))
//			{
//				var _class:Class=Class(ApplicationDomain.currentDomain.getDefinition("btn_Spin"));
//				var btn:*=new _class();
//				this.addChild(btn);
//			}


		}

		override public function initGame():void
		{
			super.initGame();
			trace("资源加载完，主游戏界面StageGame.StageGame()");
			var bitmap:Bitmap=new Bitmap(BitmapManager.instance.roleBitmapDict["view/img/texas_bg.png"] as BitmapData);
			this.addChild(bitmap);
			//ui弹出层
			UIPopupLayer.I;

			//游戏层
			this.gameLayer=new GameLayer(null, null);
			this.addChild(gameLayer);
			LayerManager.instance.layerDict[GameLayer.NAME]=this.gameLayer;


			//主UI
			this.mainUI=new UIMain();
			addChild(mainUI);
			LayerManager.instance.layerDict[UIMain.NAME]=this.mainUI;
			this.refreshInterval=30;
			GameData.gameRefreshInterval=this.refreshInterval;

			//最后加UI弹出层
			this.addChild(UIPopupLayer.I);
			//设置人物数据显示
			//RoleManager.instance.setRoleUIDisplay();

			//PathTrack.I;
			//this.addChild(PathTrack.I);
			//PathTrack.I.x=stage.stageWidth / 2;
			//PathTrack.I.y=stage.stageHeight / 2;
			//PathTrack.I.hide();

		}

		private function playFlash(flashName:String, event:MouseEvent):void
		{
			var mc:MovieClip=BitmapManager.instance.getMCByName(flashName);
			if (!mc)
			{
				return;
			}
			this.addChild(mc);
			mc.x=event.localX;
			mc.y=event.localY;
			mc.gotoAndPlay(1);
			mc.addFrameScript(mc.totalFrames - 1, playEndHandler);
			mc.addEventListener(MouseEvent.CLICK, onMcClickHandler);
			function playEndHandler():void
			{
				mc.gotoAndStop(mc.totalFrames - 1);
				mc.addFrameScript(0, null);
				if (mc.parent)
				{
					mc.parent.removeChild(mc);
					mc=null;
				}
			}
			function onMcClickHandler(event:MouseEvent):void
			{
				trace("click mc");

			}
		}



		override public function clickHandler(event:MouseEvent):void
		{
			if (event.target != this){
				return;
			}				
			if (event.target == this)
			{
//				playFlash("skill1", event);
//				playFlash("PlayButton", event);
				/*var mc:MovieClip=BitmapManager.instance.getMCByName("skill1");
				if (!mc)
				{
					return;
				}
				UIPopupLayer.I.show(mc);
				mc.x=event.localX;
				mc.y=event.localY;
				mc.gotoAndPlay(1);
				mc.addFrameScript(mc.totalFrames - 1, playEndHandler);
				mc.addEventListener(MouseEvent.CLICK, onMcClickHandler);
				function playEndHandler():void
				{
					mc.gotoAndStop(mc.totalFrames - 1);
					mc.addFrameScript(0, null);
					if (mc.parent)
					{
						mc.parent.removeChild(mc);
						mc=null;
					}
				}
				function onMcClickHandler(event:MouseEvent):void
				{
					trace("click mc");

				}*/
			}
			var task:UITaskList=new UITaskList();
			UIPopupLayer.I.show(task);
//			playFlash("mc_loading");			
//			this.gameLayer.clickHandler(event);
//			trace(event.target);
		}

		override public function refreshHandler(event:TimerEvent):void
		{
			if (this.gameLayer)
			{
				this.gameLayer.update();
			}
		}

		/**
		 * 窗口大小改变
		 * @param event
		 */
		override public function resizeHandler(event:Event):void
		{
			if (this.gameLayer)
			{
				this.gameLayer.resize();
			}

			if (this.mainUI)
			{
				this.mainUI.resize();
			}

			if (UIPopupLayer.I)
			{
				UIPopupLayer.I.resize();
			}
//
//			if (PathTrack.I)
//			{
//				PathTrack.I.resize();
//			}
		}



		override public function mouseDownHandler(event:MouseEvent):void
		{
			this.gameLayer.mouseDownHandler(event);
//			this.gameLayer.startDrag();
		}

		override public function mouseUpHandler(event:MouseEvent):void
		{
			this.gameLayer.mouseUpHandler(event);
//			this.gameLayer.stopDrag();
		}
	}
}
